const api = require('../../../../wxapi/config.js')
var app = getApp()
Page({
	/**
 		* 用户点击右上角分享
 	*/
	onShareAppMessage: function () {
		return {
			title: '影视行业大数据',
			path: '/pages/index/RankingList/thermal/thermal'
		}
	},
	/**
	 * 页面的初始数据
	 */
	data: {
    cloudList: [],
    isfirstloading: true,
    items: [],
    paraUp: {
      id: '',
      rq0: ''
    },
    paraDown: {
      id: '',
      rq0: ''
    },
    pageSizeUp: -10,
    pageSizeDown: 10
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    
	},
  getTableList(id, date, size) {
    let textMap = { 10: "已是最新数据", '-10': "没有更多数据了" }
    api.getEntertainmentHotspotsTable({ newsId: id, rq: date, pageSize: size }).then((res) => {
      if (id === '' && date === '' && 0 === res.data.length) {
        wx.showToast({
          title: '无数据显示',
          icon: 'none',
          duration: 2500,
          success: function () {

          }
        })
      } else if (0 === res.data.length) {
        wx.showToast({
          title: textMap[size],
          icon: 'none',
          duration: 2500,
          success: function () { }
        })
      } else {
        let dataArr = res.data
        let len = dataArr.length
        if ('' === id && '' === date) {
          this.setData({
            isfirstloading: false,
            items: dataArr,
            paraUp: dataArr[len - 1],
            paraDown: dataArr[0]
          })
        } else {
          if (10 === size) { // 向下滑 加载时间靠后（最新）数据
            let tempArr = dataArr.reverse()
            for (let i in tempArr) {
              this.data.items.unshift(tempArr[i])
            }
            this.setData({
              items: this.data.items
            })
            this.setData({
              paraDown: this.data.items[0]
            })
          }
          if (-10 === size) { // 向上滑 加载时间靠前数据
            for (let i in dataArr) {
              this.data.items.push(dataArr[i])
            }
            this.setData({
              items: this.data.items,
              paraUp: dataArr[len - 1]
            })
          }
        }
      }
    })
  },
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
    if (app.globalData.isReload.thermal) {
      wx.pageScrollTo({
        scrollTop: 0
      })
      this.getTableList('', '', 10)
    } else {
      app.globalData.isReload.thermal = true
    }
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
    this.getTableList(this.data.paraDown.id, this.data.paraDown.rq0, this.data.pageSizeDown)
    wx.stopPullDownRefresh()
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
    this.getTableList(this.data.paraUp.id, this.data.paraUp.rq0, this.data.pageSizeUp)
	}
})